package com.comleader.operator.validate;

import static cn.hutool.core.text.CharSequenceUtil.*;
import static com.comleader.operator.common.constant.Constant.*;
import static com.comleader.operator.common.constant.DictConstant.*;
import static com.comleader.operator.validate.CommonValidator.*;

import java.util.function.Function;

import com.comleader.operator.pojo.entity.IovCardEntity;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Dscription 短信端口校验器
 * @Author 连新景
 * @Date 2024/08/05 11:05
 */
@Slf4j
public class IovCardValidator {

    /*
     * @Description 校验格式异常
     * @Auhtor 连新景
     * @Date 2024/08/05 11:08
     * @param entity 
     * @return boolean
     */
    public static boolean isFormatException(IovCardEntity entity) {
        String userType = entity.getUserType();
        // 必填项判断
        if (hasBlank(userType, entity.getVin(), entity.getEngineNo(), entity.getBrand(), entity.getVehicleModel(), entity.getBodyColor(),
            entity.getUserNumber(), entity.getNumberAcceptanceTime(), entity.getNumberStatus(), entity.getVoiceEnableStatus(), entity.getSmsEnableStatus(),
            entity.getTrafficEnableStatus(), entity.getCardType(), entity.getChannelType())) {
            return true;
        }
        // 校验字段的值是否在字典配置中已配置
        if (isDictIncorrect(USER_TYPE, entity.getUserType()) || isDictIncorrect(CARD_TYPE, entity.getCardType())) {
            return true;
        }
        // 语音
        if (OPEN.equals(entity.getVoiceEnableStatus())) {
            if (isBlank(entity.getVoiceLimitType())) {
                return true;
            }
            if (DIRECTIONAL.equals(entity.getVoiceLimitType())) {
                if (isBlank(entity.getVoiceDirectNum())) {
                    return true;
                }
            }
        }
        // 短信
        if (OPEN.equals(entity.getSmsEnableStatus())) {
            if (isBlank(entity.getSmsLimitType())) {
                return true;
            }
            if (DIRECTIONAL.equals(entity.getSmsLimitType())) {
                if (isBlank(entity.getSmsDirectNum())) {
                    return true;
                }
            }
        }
        // 流量
        if (OPEN.equals(entity.getTrafficEnableStatus())) {
            if (isBlank(entity.getTrafficLimitType())) {
                return true;
            }
            if (equalsAny(entity.getTrafficLimitType(), NON_DIRECTIONAL_LARGE_TRAFFIC, NON_DIRECTIONAL_SMALL_TRAFFIC)) {
                if (isBlank(entity.getNondirectTrafficLimit())) {
                    return true;
                }
            } else if (DIRECTIONAL_TRAFFIC_LIMIT.equals(entity.getTrafficLimitType())) {
                if (isBlank(entity.getTrafficDirectNum())) {
                    return true;
                }
            }
        }
        // 单位
        if (COMPANY.equals(entity.getUserType())) {
            // 校验责任人信息
            if (hasBlank(entity.getPicName(), entity.getPicIdType(), entity.getPicIdNumber(), entity.getPicIdAddress())) {
                return true;
            }
            // 校验单位信息
            if (hasBlank(entity.getCompanyName(), entity.getCompanyIdNumber(), entity.getCompanyIdType(), entity.getCompanyIdAddress())) {
                return true;
            }
            // 校验经办人
            if (hasBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress())) {
                return true;
            }
            // 个人
        } else if (PERSONAL.equals(entity.getUserType())) {
            // 校验机主
            if (hasBlank(entity.getOwnerName(), entity.getOwnerIdType(), entity.getOwnerIdNumber(), entity.getOwnerIdAddress())) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description 是否渠道类型不合规
     * @Auhtor 连新景
     * @Date 2024/09/05 9:16
     * @param channelType
     * @return boolean
     */
    public static boolean isChannelTypeNoncompliant(String channelType) {
        return isNotBlank(channelType) && isDictIncorrect(IOVCARD_CHANNEL_TYPE, channelType);
    }

    /*
     * @Description 校验单位信息未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 16:11
     * @param entity
     * @return boolean
     */
    public static boolean isCompanyUnregister(IovCardEntity entity) {
        return isAllBlank(entity.getCompanyName(), entity.getCompanyIdType(), entity.getCompanyIdNumber(), entity.getCompanyIdAddress(),
            entity.getCompanyMailingAddress());
    }

    /**
     * @Description 校验责任人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isPicUnregister(IovCardEntity entity) {
        return isAllBlank(entity.getPicName(), entity.getPicIdType(), entity.getPicIdNumber(), entity.getPicIdAddress(), entity.getPicMailingAddress());

    }

    /**
     * @Description 校验经办人未登记
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isAgentUnregister(IovCardEntity entity) {
        return isAllBlank(entity.getAgentName(), entity.getAgentIdType(), entity.getAgentIdNumber(), entity.getAgentIdAddress(),
            entity.getAgentMailingAddress());
    }

    /*
     * @Description 校验单位信息不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 10:42
     * @param entity
     * @param customerType
     * @param userBusinessType
     * @return boolean
     */
    public static boolean isCompanyNoncompliant(IovCardEntity entity) {
        return isAddressNoncompliant(entity.getCompanyIdAddress());
    }

    /**
     * @Description 校验责任人不合规
     * @Auhtor 连新景
     * @Date 2024/08/05 11:16
     * @param entity
     * @return boolean
     */
    public static boolean isPicNoncompliant(IovCardEntity entity) {
        return BooleanUtil.or(isNotBlank(entity.getPicName()) && !Validator.isChineseName(entity.getPicName()),
            isNotBlank(entity.getPicIdType()) && isDictIncorrect(IOVCARD_IDCARD_TYPE, entity.getPicIdType()),
            RESIDENT_IDCARD.equals(entity.getPicIdType()) && isNotBlank(entity.getPicIdNumber()) && !IdcardUtil.isValidCard(entity.getPicIdNumber()),
            isAddressNoncompliant(entity.getPicIdAddress()));

    }

    /**
     * @Description 校验经办人不合规
     * @Auhtor 连新景
     * @Date 2024/08/05 14:36
     * @param entity
     * @return boolean
     */
    public static boolean isAgentNoncompliant(IovCardEntity entity) {
        return BooleanUtil.or(isNotBlank(entity.getAgentName()) && !Validator.isChineseName(entity.getAgentName()),
            isNotBlank(entity.getAgentIdType()) && isDictIncorrect(IOVCARD_IDCARD_TYPE, entity.getAgentIdType()),
            RESIDENT_IDCARD.equals(entity.getAgentIdType()) && isNotBlank(entity.getAgentIdNumber())
                && !IdcardUtil.isValidCard(entity.getAgentIdNumber()),
            isAddressNoncompliant(entity.getAgentIdAddress()));
    }

    /**
     * @Description 是否未登记开通非定向功能
     * @Auhtor 连新景
     * @Date 2024/08/19 16:14
     * @param entity
     * @return boolean
     */
    public static boolean isUnregisterNondirectFunction(IovCardEntity entity) {
        return BooleanUtil.or(OPEN.equals(entity.getVoiceEnableStatus()) && isBlank(entity.getVoiceLimitType()),
            OPEN.equals(entity.getSmsEnableStatus()) && isBlank(entity.getSmsLimitType()),
            OPEN.equals(entity.getTrafficEnableStatus()) && isBlank(entity.getTrafficLimitType()));
    }

    /**
     * @Description 是否未登记车辆信息
     * @Auhtor 连新景
     * @Date 2024/08/19 16:22
     * @param entity
     * @return boolean
     */
    public static boolean isUnregisterVehicleInfo(IovCardEntity entity) {
        return isAllBlank(entity.getVin(), entity.getEngineNo(), entity.getBrand(), entity.getVehicleModel(), entity.getBodyColor());
    }

    /**
     * @Description 是否限制类型登记错误
     * @Auhtor 连新景
     * @Date 2024/08/20 10:30
     * @param dictCode
     * @param entity
     * @param enableStatusFunction
     * @param limitTypeFunction
     * @return boolean
     */
    public static boolean isLimitTypeRegisterError(IovCardEntity entity, String dictCode, Function<IovCardEntity, String> enableStatusFunction,
        Function<IovCardEntity, String> limitTypeFunction) {
        return OPEN.equals(enableStatusFunction.apply(entity)) && isNotBlank(limitTypeFunction.apply(entity))
            && isDictIncorrect(dictCode, limitTypeFunction.apply(entity));
    }

    /**
     * @Description 是否定向数量登记错误
     * @Auhtor 连新景
     * @Date 2024/08/20 10:32
     * @param entity
     * @param enableStatusFunction
     * @param limitTypeFunction
     * @param directNumberFunction
     * @return boolean
     */
    public static boolean isDirectNumRegisterError(IovCardEntity entity, Function<IovCardEntity, String> enableStatusFunction,
        Function<IovCardEntity, String> limitTypeFunction, Function<IovCardEntity, String> directNumberFunction, String limitType) {
        return OPEN.equals(enableStatusFunction.apply(entity)) && limitType.equals(limitTypeFunction.apply(entity))
            && !NumberUtil.isNumber(directNumberFunction.apply(entity));
    }

    /**
     * @Description 是否定向数量超限（语音、短信、流量共用方法）
     * @Auhtor 连新景
     * @Date 2024/08/20 9:39
     * @param paramKey
     * @param entity
     * @param enableStatusFunction
     * @param limitTypeFunction
     * @param directNumberFunction
     * @return boolean
     */
    public static boolean isDirectNumOverLimit(IovCardEntity entity, String paramKey, Function<IovCardEntity, String> enableStatusFunction,
        Function<IovCardEntity, String> limitTypeFunction, Function<IovCardEntity, String> directNumberFunction, String limitType) {
        if (!OPEN.equals(enableStatusFunction.apply(entity)) || !limitType.equals(limitTypeFunction.apply(entity))
            || !NumberUtil.isNumber(directNumberFunction.apply(entity))) {
            return false;
        }
        String threshold = selectValueByKey(paramKey);
        if (!NumberUtil.isNumber(threshold)) {
            return false;
        }
        return Convert.toInt(directNumberFunction.apply(entity)) >= Convert.toInt(threshold);
    }

}
